<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        li {
            list-style: none;
        }

        .wrap {
            width: 600px;
            margin: 50px auto
        }

        .nav {
            display: flex;
            line-height: 40px;
            text-align: center;
        }

        .nav li {
            flex: 1;
            border: 1px solid #333;
        }

        /* .nav li:not(:first-child) {
            border-left: 0;
        } */

        .nav li+li {
            border-left: 0;
        }

        .active {
            background-color: #6ff;
            color: #fff;
        }

        .part {
            width: 100%;
            height: 400px;
        }

        .part .part-item {
            height: 400px;
            text-align: center;
            line-height: 400px;
            border: 1px solid #333;
            display: none;
        }

        .part .show {
            display: block;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <ul class="nav">
            <li class="active">nav_01</li>
            <li>nav_02</li>
            <li>nav_03</li>
            <li>nav_04</li>
            <li>nav_05</li>
            <li>nav_06</li>
        </ul>
        <div class="part">
            <div class="part-item show">part_01</div>
            <div class="part-item">part_02</div>
            <div class="part-item">part_03</div>
            <div class="part-item">part_04</div>
            <div class="part-item">part_05</div>
            <div class="part-item">part_06</div>
        </div>
    </div>
</body>
<script>

    var nav = document.getElementsByClassName("nav")[0];
    var liList = nav.getElementsByTagName("li");  // [li,li,li,li,li,li]

    var partItems = document.querySelectorAll(".part .part-item");


    // 导航栏切换: 
    // 1. 被点击的导航变为活跃状态(active),之前被点击的元素清除活跃状态
    // 2. 我也不想知道之前哪一个被点击了,把所有的导航 活跃状态全部清除 再把自己变为活跃状态



    /*   for (var i = 0; i < liList.length; i++) {  // i=> 0-5
          var li = liList[i];  //获取对应下标的元素
          // 页面加载时 遍历每一个元素 => 绑定自定义属性
          li.setAttribute("data-index", i);// 0 1 2
          li.onclick = function () {
              var index = this.dataset.index;
              console.log(this, index);
  
              for (var j = 0; j < liList.length; j++) {
                  liList[j].className = ""; // 清除每一个导航的class名
                  // partItems[j].className = "part-item";
                  partItems[j].classList.remove("show");
              }
  
              liList[index].className = "active";
              // partItems[index].className = "part-item show";
              partItems[index].classList.add("show");
          }
      } */



    for (let i = 0; i < liList.length; i++) {  // i=> 0-5
        let li = liList[i];  //获取对应下标的元素
        li.onclick = function () {
            for (var j = 0; j < liList.length; j++) {
                liList[j].className = ""; // 清除每一个导航的class名
                // partItems[j].className = "part-item";
                partItems[j].classList.remove("show");
            }
            liList[i].className = "active";
            // partItems[i].className = "part-item show";
            partItems[i].classList.add("show");
        }
    }





</script>

</html>